{extend name="common/base" /}

{block name="style"}
<style type="text/css">
    body{
        background: #EB334F;
    }
</style>
{/block}
{block name="toindex"}
{/block}
{block name="body"}
<div class="lqyhq-mains">
    <div class="lqyhq-list">
        <div class="lqyhq-list-div">
            <img src="{$user_info.avatar}" class="hq-img">
            <ul class="lqyhq-nav">
                <li>
                    <div class="lqy-nav-div">手机号</div>
                    <input type="number" class="lay-nav-input js-phone" placeholder="请输入11位手机号码">
                    <div class="lqy-nav-div2 js-yzm" is_allow_click='1'>获取验证码</div>
                </li>
                <li>
                    <div class="lqy-nav-div">验证码</div>
                    <input type="number" class="lay-nav-input js-vcode" placeholder="请输入4位验证码">
                </li>              
            </ul>
            <div class="lqyhq-button">
                {eq name="is_shiming" value="0"}
                <div class="div1 js-submit">领取优惠券</div>
                {else/}
                <div class="div1 js-submit">下一步</div>
                {/eq}
            </div>
            <!--<div class="lqyhq-lqcg">领取成功</div>-->
        </div>
    </div>
</div>

{/block}
{block name="script"}
<script>
    $('.js-submit').on('click', function () {
        var phone = $('.js-phone').val();
        var vcode = $('.js-vcode').val();

        if (phone == '') {
            mui.toast("请填写手机号");
        }

        if (vcode == '') {
            mui.toast("请填写验证码");
        }

        $.post("{:url('bindphone')}", {phone: phone, vcode: vcode}, function (res) {
            if (res.code == '1') {
                mui.toast(res.msg);
                location.href = res.data.url;
            } else {
                mui.toast(res.msg);
            }
        }, 'json');

    });
    //发送验证码
    $('.js-yzm').click(function () {
        var phone = $('.js-phone').val();
        if (!is_mobile(phone)) {
            mui.toast('请填写正确格式手机号');
            return false;
        }
        var obj = $(this);
        if (obj.attr('is_allow_click') == 1) {
            settime($(this));
            $.post("{:url('pub/sendVcode')}", {phone: phone}, function (res) {
                $.toast(res.msg);
            }, 'json');
        }
    });

    //验证码发送
    var countdown = 60;
    function settime(obj) { //发送验证码倒计时
        console.log(countdown);
        if (countdown == 0) {
            obj.attr('is_allow_click', 1);
//            obj.attr('disabled', false);
            //obj.removeattr("disabled"); 
            obj.text("获取验证码");
            countdown = 60;
            return;
        } else {
            obj.attr('is_allow_click', 0);
//            obj.attr('disabled', true);
            obj.text("重新发送(" + countdown + ")");
            countdown--;
        }
        setTimeout(function () {
            settime(obj);
        }, 1000);
    }
</script>
{/block}